#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) {
        struct ListNode* curA = headA, * curB = headB;
        int Len_A = 1;
        int Len_B = 1;
        while (curA->next)
        {
            curA = curA->next;
            Len_A++;
        }

        while (curB->next)
        {
            curB = curB->next;
            Len_B++;
        }

        if (curA != curB)
        {
            return NULL;
        }

        struct ListNode* shortList = headA, * longList = headB;
        if (Len_A > Len_B)
        {
            shortList = headB;
            longList = headA;
        }

        int gap = abs(Len_B - Len_A);

        while (gap--)
        {
            longList = longList->next;
        }

        while (shortList != longList)
        {
            shortList = shortList->next;
            longList = longList->next;
        }

        return longList;
    }
};